package com.hx.hospital.mapper;

import com.hx.hospital.pojo.Department;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface DepartmentMapper extends Mapper<Department> {

    @Results(id = "DepartmentResult",
            value = {@Result(property = "id", column = "id", jdbcType = JdbcType.BIGINT),
                     @Result(property = "hospitalDistrictId", column = "hospital_district_id", jdbcType = JdbcType.BIGINT),
                     @Result(property = "departmentName", column = "department_name", jdbcType = JdbcType.VARCHAR),
                     @Result(property = "departmentId", column = "department_id", jdbcType = JdbcType.BIGINT),
                     @Result(property = "hospitalName", column = "hospital_name", jdbcType = JdbcType.VARCHAR),
                     @Result(property = "detailedDepartmentName", column = "detailed_department_name", jdbcType = JdbcType.VARCHAR),
                     @Result(property = "detailedDepartmentId", column = "detailed_department_id", jdbcType = JdbcType.BIGINT),
            })
    @Select("SELECT\n" +
            "d.id,\n" +
            "hd.hospital_name AS hospitalName,\n" +
            "d.department_name AS departmentName,\n" +
            "dd.detailed_department_name AS detailedDepartmentName,\n" +
            "d.hospital_district_id,\n" +
            "dd.department_id,\n" +
            "dd.id AS detailedDepartmentId\n" +
            "FROM\n" +
            "department AS d\n" +
            "LEFT JOIN detailed_departments AS dd ON d.id = dd.department_id\n" +
            "LEFT JOIN hospital_district AS hd ON d.hospital_district_id = hd.id\n" +
            "WHERE\n" +
            "d.id is not NULL")
    public List<Department> findDepartmentAll();


    @Select("SELECT\n" +
            "d.id,\n" +
            "hd.hospital_name AS hospitalName,\n" +
            "d.department_name AS departmentName,\n" +
            "dd.detailed_department_name AS detailedDepartmentName,\n" +
            "d.hospital_district_id,\n" +
            "dd.department_id,\n" +
            "dd.id AS detailedDepartmentId\n" +
            "FROM\n" +
            "department AS d\n" +
            "LEFT JOIN detailed_departments AS dd ON d.id = dd.department_id\n" +
            "LEFT JOIN hospital_district AS hd ON d.hospital_district_id = hd.id\n" +
            "WHERE\n" +
            "d.id =#{id}")
    @ResultMap("DepartmentResult")
    public List<Department> findDepartmentById(@Param("id") Long id);
}
